import React, { useState } from 'react'
import { Button, Toast } from 'antd-mobile'
import { withRouter } from 'react-router-dom'
import { chinaNetFastApply } from '../../request/home'
import './style.scss'
Toast.config({ duration: 2, mask: false })

function ChinaNet(props) {
  const orderNo = props.match.params.orderNo
  const searchParams = new URLSearchParams(window.location.search)
  const productCode = searchParams.get('code')
  const [isLoading, setIsLoading] = useState(false)

  const onSubmit = async() => {
    setIsLoading(true)
    const { code, message: errMsg } = await chinaNetFastApply({ order_no: orderNo.split('?')[0] })
    if (code) {
      setIsLoading(false)
      return Toast.fail(errMsg)
    }
    props.history.push({ pathname: `/success/${productCode}?from=chinanet` })
  }

  return (
    <div className="chinanet">
      <div className="top-img">
        <img src={require('../../assets/images/chinanet-2.jpeg')} alt=""/>
      </div>
      <div className="container">
        <div className="button-wrapper">
          <div className="left">
            <img onClick={onSubmit} src={require('../../assets/images/chinanet-apply.png')}/>
          </div>
          <div className="right">
            <img onClick={() => props.history.replace(`/?code=${productCode}`)} src={require('../../assets/images/chinanet-reject.png')}/>
          </div>
        </div>
      </div>
    </div>
  )
}

export default withRouter(ChinaNet)
